package com.example.hemin.fnb.ui.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.hemin.fnb.R;
import com.example.hemin.fnb.ui.activity.UserAbout;
import com.example.hemin.fnb.ui.bean.BaseObjectBean;
import com.example.hemin.fnb.ui.bean.ClassListBean;
import com.example.hemin.fnb.ui.bean.ClassSItem;
import com.example.hemin.fnb.ui.bean.MessageBean1;
import com.example.hemin.fnb.ui.bean.ZZSItem;
import com.example.hemin.fnb.ui.net.RetrofitClient;
import com.example.hemin.fnb.ui.util.AppUtils;
import com.example.hemin.fnb.ui.util.Utils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import me.drakeet.multitype.ItemViewBinder;

public class ClassSAdapter extends ItemViewBinder<ClassSItem, ClassSAdapter.FindClassHolder> {
    private Map Authorization = new HashMap();
    List<ClassListBean.DataBean.RecordsBean> list = new ArrayList<>();
    FindClassAdapter adapter = new FindClassAdapter(R.layout.message_adapter, list);
    private int ZPageSize = 1;

    @NonNull
    @Override
    protected ClassSAdapter.FindClassHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.messages_adapter, parent, false);
        return new FindClassHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull final ClassSAdapter.FindClassHolder holder, @NonNull final ClassSItem item) {
        holder.title.setText("课堂");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(AppUtils.getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        holder.recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new FindClassAdapter(R.layout.message_adapter, item.bean);
        holder.recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(AppUtils.getContext(), UserAbout.class);
                intent.putExtra("path", item.bean.get(position).getClassroomContent());
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                AppUtils.getContext().startActivity(intent);
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        holder.smartRefreshLayout.setEnableRefresh(false);
        holder.smartRefreshLayout.setRefreshHeader(new ClassicsHeader(AppUtils.getContext()));
        holder.smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                ZPageSize++;
                Observable cm = RetrofitClient.getInstance().getApi().getListClass(Utils.getAuthorization(AppUtils.getContext()), ZPageSize, 10);
                cm.subscribeOn(Schedulers.io()).subscribeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer() {
                    @Override
                    public void accept(Object o) throws Exception {
                        BaseObjectBean baseObjectBean = (BaseObjectBean) o;
                        Authorization = Utils.getAuthorization(AppUtils.getContext());
                        if (baseObjectBean.getData() instanceof  ClassListBean.DataBean) {
                            ClassListBean.DataBean beam = (ClassListBean.DataBean) baseObjectBean.getData();
                            List<ClassListBean.DataBean.RecordsBean> list = beam.getRecords();
                            adapter.addData(list);

                            holder.smartRefreshLayout.finishLoadMore(100);


                        }
                    }
                });
            }
        });

    }

    public class FindClassHolder extends RecyclerView.ViewHolder {
        RecyclerView recyclerView;
        SmartRefreshLayout smartRefreshLayout;
        TextView title;

        public FindClassHolder(@NonNull View view) {
            super(view);
            recyclerView = view.findViewById(R.id.mazhi_recyclerview);
            smartRefreshLayout = view.findViewById(R.id.smartRefresh);
            title = view.findViewById(R.id.title);

        }
    }
}
